﻿@using MvcContrib.UI.Grid
@using XRM.Security.WebUI.MvcHelpers
@model XRM.Security.WebUI.Models.UserSkzi.UserSkziListModel
@{
    ViewBag.Title = "Реестр СКЗИ";
    ViewBag.HideHeader = true;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section styles
{
    @Styles.Render("~/bundles/jquery-css")
}

<div>
    @using (Html.BeginForm("Filter", "UserSkzi", FormMethod.Post, new { @class = "form-horizontal form-horizontal-2", id = "filterForm" }))
    {
        <div class="form-group">
            @Html.LabelFor(m => m.TextFilter, new { @class = "control-label" })
            <div class="control-group">
                    <div class="col-sm-5">
                        <div class="input-group">
                            @Html.TextBoxFor(m => m.TextFilter, new { @class = "form-control", title = "Введите ФИО пользователя, номер экземпляра СКЗИ или серийный номер ПК" })
                            @Html.Partial("ClearButtonPartial")
                        </div>
                    </div>
                    <div class="col-sm-1">
                        <button class="btn btn-primary btn-block" type="submit" id="btnFind">Найти</button>
                    </div>
            </div>
        </div>
        
        <div class="form-group">
                <label class = "control-label">&nbsp;</label>
                <div class="control-group">
                    <div class="col-sm-5">
                        <div class="checkbox">
                            <label for="ShowAllFilter">
                                @Html.CheckBoxFor(x => x.ShowAllFilter)
                                Искать среди удаленных
                            </label>
                        </div>    
                    </div>
                </div>
        </div>
    }
    <hr/>

    <p class="actions-centered">
        @Html.DialogFormButton(new DialogButtonOptions()
   {
       Text = "Добавить СКЗИ",
       ContentUrl = Url.Action("AddUserSkziDialog", "UserRegister"),
       Width = "800",
       Id = "dlgAddUserSkziDialog",
       ButtonCss = "btn btn-large btn-primary",
       ButtonId = "btnAddUserSkzi",
       OnOpen = "BindDialogButtons",
       OnSuccess = "RefreshPage"
   })
    </p>
</div>

    @{
        if (Model.SkziList.Any())
        {
            Html.Grid(Model.SkziList).Columns(column =>
            {
                column.For(item => Html.ActionLink(item.UserRegisterFullName, "Edit", "UserRegister", new { id = item.UserRegisterId }, null)).Named("Пользователь");
                column.For(item => item.SkziKindName).Named("Вид СКЗИ");
                column.For(item => item.CopyNumber).Named("Номер экземпляра СКЗИ");
                column.For(item => item.PCSerialNumber).Named("Серийный номер ПК");
                column.For(item => item.SetupDate.ToString("dd.MM.yyyy")).Named("Дата установки");
                
                column.Custom(@<text>
                                    <input class="hidden" type="text" value="@(item.DeleteDate.HasValue?item.DeleteDate.Value.ToString("yyyy.MM.dd"):"0")" />
                                    @if (item.DeleteDate.HasValue)
                                    {
                                        <text>@item.DeleteDate.Value.ToString("dd.MM.yyyy")</text>
                                        <i class="fa fa-comment" rel="tooltip" title="@item.DeleteReason" data-placement="top"></i>
                                    }
                              </text>).Named("Дата удаления");
                                     
                column.Custom(@<text>
                                   @if(item.DeleteDate == null)
                                   {
                                   @Html.DialogFormButton(new DialogButtonOptions
                                       {
                                               Text = "Удалить СКЗИ",
                                               ContentUrl = Url.Action("DeleteUserSkziDialog", "UserRegister", new { userSkziId = item.Id }),
                                               Width = "800",
                                               Id = "dlgDeleteUserSkziDialog",
                                               ButtonId = "btnDeleteUserSkzi",
                                               Image = "fa-trash-o",
                                               OnSuccess = "RefreshPage"
                                       })
                                   }
                               </text>)
                      .Named("")
                      .Attributes(Class => "single-action");


            }).Attributes(@class => "table table-bordered", id => "table_id")
            .RowAttributes(item => new MvcContrib.Hash(@class => item.Item.DeleteDate.HasValue ? "inactive" : ""))
            .Empty("Нет данных для отображения").Render();

        } 
    }

@section History
{
    <div class="panel panel-default">
        <div class="panel-heading">
            <i class="fa fa-list-alt fa-fw"></i>
            История
        </div>
        <div class="panel-body" id="historyDiv">
            @{Html.RenderAction("History", new { page = 1 });}    
            @Html.Partial("LoadHistoryButton")
        </div>
    </div>
}

@section scripts
{
    @Scripts.Render("~/bundles/gridlibrary")
    <script type="text/javascript" src="@(Url.Content("~/Scripts/AjaxRequest.js"))"></script>    
    <script type="text/javascript">
        $(document).ready(function () {
            @if (Model.SkziList.Any())
            {
                <text>
                $('#table_id').dataTable({
                    paging: false,
                    bFilter: false,
                    bSort: true,
                    info: false,
                    "order": [[0, "asc"]],
                    "columnDefs": [{ "type": "date-eu", targets: 4 }, { "orderDataType": "dom-text", targets: 5 }, { 'bSortable': false, 'aTargets': [6] }]
                });
                </text>
            }

            PrepareAutocompleteEdit("TextFilter", '@(Url.Action("FilterAutocomplete"))');
        });

        function RefreshPage() {
            window.location = window.location;
        }
    </script>
}